<?php


/**
 * 下午茶咖啡生产查询类
 * @package  htdocs 
 * @subpackage  quanliadmin
 * @author   allen.qiang  <allen.qiang@fistforward.com> 2015-01-20 allen.qiang FUN-AC-0028-下午茶项目
 * @version 1.0
 */

class sysCoffee{


    /**
     * 获取下午茶申请单信息
     * @param $arg_array
     * @return array|null
     */
    public function getCoffeeList(&$arg_array){
    	global $G_PGLOB;
        //sql字符串
        $sql = ' AND billPlatform=3 ';
        
        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //状态
        if (isset($arg_array['bill_status']) && is_numeric($arg_array['bill_status']) ) {
            if($arg_array['bill_status']==1)
            {
                $sql .= ' AND bill_status = ?';
                $params_type .= 'i';
                $params_array[] = $arg_array['bill_status'];
            }
            else if($arg_array['bill_status']==2)
            {
                $sql .= ' AND bill_status IN (2,3,4,5,6,7) ';
            }
        }else{
            $sql .= ' AND bill_status IN (1,2,3,4,5,6,7) ';
        }

        //订单编号
        if (isset($arg_array['bill_code']) && !empty($arg_array['bill_code'])) {
            $sql .= ' AND bill_code = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['bill_code'];
        }

        //配送日期
        if (isset($arg_array['send_date']) && !empty($arg_array['send_date'])) {
            $sql .= ' AND send_date = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['send_date'];
        }

        //配送城市ID
        if (isset($arg_array['deliver_cityid']) && !empty($arg_array['deliver_cityid'])) {
            $sql .= ' AND deliver_cityid = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['deliver_cityid'];
        }

        //配送站
        if (isset($arg_array['distribution_id']) && !empty($arg_array['distribution_id'])) {
            $sql .= ' AND distribution_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['distribution_id'];
        }

        if ($sql) {
            $sql = ' WHERE ' . substr($sql, 4) ;
        }

        try {

            if (isset($arg_array['rp'])) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM bill_master  ' . $sql;
                $count_array = &$G_PGLOB->DBCA->stmt_query($search_sql, $params_type, $params_array);

                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            $sql=str_replace("WHERE","HAVING",$sql);

            //查询分类信息
            //$search_sql = "SELECT a.*,GROUP_CONCAT((CONCAT(b.`goods_name`,'：', b.`goods_amount`)) SEPARATOR '，')AS product_detail  FROM  bill_master a LEFT JOIN `bill_goods` b ON a.bill_no=b.bill_no GROUP BY a.bill_no " . $sql;

            $search_sql = "SELECT * FROM bill_master "  . $sql;

            //error_log(print_r($search_sql,1));


            $temp_item_array = &$G_PGLOB->DBCA->stmt_query($search_sql, $params_type, $params_array);

            if (!$temp_item_array)
                return null;

            $item_array = array();

            foreach($temp_item_array as &$item) {
                //edit by junhui.li 2015-04-09 start
                $dunchSql="select * from dunch_treat where bill_no=?";
                $dunchSqlResult=&$G_PGLOB->DBCA->stmt_query($dunchSql,'s',$item->bill_no);



                if(empty($dunchSqlResult)){
                    $item_array[] = array(
                        'creator' => $item->creator,
                        'createTime' => $item->createTime,
                        'bill_no' => $item->bill_no,
                        'bill_code' => $item->bill_code,
                        'bill_status' => $item->bill_status,
                        'distribution_id' => $item->distribution_id,
                        'distribution_name' => $item->distribution_name,
                        'send_date' => $item->send_date,
                        'send_start_time' => $item->send_start_time,
                        'send_end_time' => $item->send_end_time,
                        'deliver_cityid' => $item->deliver_cityid,
                        'product_detail' => $item->goods_name_str,
                        'deliver_name' => decryptD($item->deliver_name)//add by allen.qiang 20150204 2.另新增需求: 下午茶咖啡生产查询中需添加字段“收货人姓名”，用于咖啡制作的同事能根据此信息在咖啡杯上给客户书写个性化的问候
                    );
                }else{
                    if($dunchSqlResult[0]->billMark==1){
                        $item_array[] = array(
                            'creator' => $item->creator,
                            'createTime' => $item->createTime,
                            'bill_no' => $item->bill_no,
                            'bill_code' => $item->bill_code,
                            'bill_status' => $item->bill_status,
                            'distribution_id' => $item->distribution_id,
                            'distribution_name' => $item->distribution_name,
                            'send_date' => $item->send_date,
                            'send_start_time' => $item->send_start_time,
                            'send_end_time' => $item->send_end_time,
                            'deliver_cityid' => $item->deliver_cityid,
                            'product_detail' => $item->goods_name_str,
                            'deliver_name' => decryptD($item->deliver_name)//add by allen.qiang 20150204 2.另新增需求: 下午茶咖啡生产查询中需添加字段“收货人姓名”，用于咖啡制作的同事能根据此信息在咖啡杯上给客户书写个性化的问候
                        );
                    }else{

                        --$arg_array['count'];
                        continue;
                    }
                }
                //edit by junhui.li 2015-04-09 end

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }


    //咖啡生产
    public function coffeeProduce(){
        global $G_PGLOB;
        quanli_include('module/base/bill/billEdit.class.php');
        //实例日志类
        $quanliLog = new quanliLog();

        if (!isset($_REQUEST['general_goods'])) {
            return array( 'code' => 400, 'message' => '请选择要生产的单据' );
        }



        try{
            $G_PGLOB->DBCA->beginTransaction();

            //改变订单生产状态
            foreach($_REQUEST['general_goods'] as $item) {

                //查询订单修改状态
                $bill_master = new entity('bill_master');
                $bill_master->bill_code = $item['bill_code'];
                $bill_master->bill_status=1;
                if($G_PGLOB->DBCA->fetch($bill_master)){
                    $bill_master->bill_status =2;
                    if (!$G_PGLOB->DBCA->save($bill_master)) {
                        throw new Exception('页面已过期，请刷新页面重试', 404);
                    }
                    //保存日志
                    $log_array = array(
                        'logCategoryID' => 1,
                        'logItem' => $bill_master->bill_no,
                        'extend_1' => '咖啡已生产',
                        'extend_5' => 2,
                        'operator' => $G_PGLOB->currentUserName
                    );
                    $quanliLog->saveLogInfo( $log_array );

                    //保存订单轨迹
                    $operator = $G_PGLOB->currentUserID;
                    $content = '咖啡已生产';
                    $billEdit = new billEdit( $G_PGLOB->DBCA);
                    $billTrace = $billEdit->addBillTrace( $bill_master->bill_no, $operator, $content );
                    if ( !$billTrace ){
                        throw new Exception( '订单轨迹保存失败', 400);
                    }
                }
            }

            $G_PGLOB->DBCA->commit();
            return array( 'code' => 200, 'message' => '生产成功！' );
        }
        catch (Exception $e) {

            $G_PGLOB->DBCA->rollBack();

            if ($e->getCode() > 0)
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            else {
                error_log($e->getMessage());
                return array( 'code' => 700, 'message' => '系统错误' );
            }
        }
    }


}